package com.fingard.dsp.bank.directbank.kjtpay01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.FGBiz;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.google.gson.Gson;
import net.sf.json.JSONArray;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class KJTPAY01Balance extends KJTPAY01Base implements IBalance {

    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        // 余额返回给ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        p_req7506.readRecord();
        Connection conn = FGBiz.getLocalDbConn();
        JSONObject sbf = new JSONObject();
        KJTPAY01Header bankHeader = new KJTPAY01Header();
        bankHeader.transCode = "query_balance_by_account_type";
        bankHeader.version = "1.0";
        // 组装请求前缀
        writeCommonHeader(sbf, bankHeader);
        // 组装请求报文体json
        JSONObject json = new JSONObject();
        json.put("identity_type","1");
        json.put("identity",p_req7506.readedDetail.actNum);
        json.put("account_type","301");
            // 发送到银行
            String[] tmpStrRet = sendToBank(sbf,json.toString(), p_req7506.reqHeader.transType, p_req7506.reqHeader.transType);
            if (tmpStrRet[0].length() == 0) {
                // 把结果转成json
                JSONObject response = JSON.parseObject(tmpStrRet[1]);
                if (response != null) {

                        String responseCode = response.getString("code");
                        String responseMsg = response.getString("msg");
                        if ("S10000".equals(responseCode)) {
                            JSONObject bodyInfos = JSON.parseObject(response.getString("biz_content"));
                            String sub_code = response.getString("sub_code");
                            String sub_msg = response.getString("sub_msg");// 查看银行返回是否有ctnkey，有则续传
                            if ("S10000".equals(sub_code)) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actNum = bodyInfos.getString("account_id");
                                tmpRetDetail.actCur = this.getBankConfig().getMapValueByKey("currencyMappingInfo",p_req7506.readedDetail.accountId);
                                String tmpAvailBal = bodyInfos.getString("available_bal ance");
                                tmpRetDetail.availBal = StringHelper.isNullOrEmpty(tmpAvailBal) ? "0.00" : tmpAvailBal;
                                String tmpFrozenBal = bodyInfos.getString("frozen_balan ce");
                                tmpRetDetail.frozenBal = StringHelper.isNullOrEmpty(tmpFrozenBal) ? "0.00" : tmpFrozenBal;
                                tmpRetDetail.currBal = String.valueOf(Long.valueOf(tmpRetDetail.availBal) +
                                        Long.valueOf(tmpRetDetail.frozenBal));
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            } else {
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo(sub_msg);
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(responseMsg);
                        }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo("请求未正常返回");
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        conn.close();
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        retAts.writeRespInfo("暂不支持历史余额查询");
        retAts.writeRespCode("-1");
        return retAts;
    }
}
